'use strict';

const Controller = require('egg').Controller;
const mongoose = require('mongoose');

class ReplyController extends Controller {
  async add() {
    const {ctx,service} = this;
    const {shoptoplvid,articleid,openid,content,atype} = ctx.request.body;
    let userInfo = await service.user.getUserByid(openid);
    let result;
    if(userInfo.data==1){
      let {nickName,avatarUrl} = userInfo.result;
      let article = await service.normal.getinfobyid('Article',articleid);
      if(article.data==1){
        if(atype==1){
          let reply = await ctx.model.Reply.findOne({articleid: articleid, openid: openid, atype: 1});
          if(reply){
            let collect_count = article.result.collect_count;
            collect_count--
            await service.normal.statebyid('Article',articleid,{collect_count});
            await ctx.model.Reply.remove({articleid: articleid, openid: openid, atype: 1});
            result = {data:1,content:'取消赞成功！'};
          }else{
            let collect_count = article.result.collect_count;
            collect_count++
            await service.normal.statebyid('Article',articleid,{collect_count});
            await service.normal.addinfo('Reply',{shoptoplvid,articleid,openid,nickName,avatarUrl,atype});
            result = {data:1,content:'点赞成功！'};
          }
        }else{
          let reply_count = article.result.reply_count;
          reply_count++
          await service.normal.statebyid('Article',articleid,{reply_count});
          result = await service.normal.addinfo('Reply',{shoptoplvid,articleid,openid,content,nickName,avatarUrl,atype});
        }
      }
    }
    ctx.body = result;
  }

  async getzan() {
    const {ctx,service} = this;
    const {articleid,openid,page,size} = ctx.request.body;
    let reply = await ctx.model.Reply.findOne({articleid: articleid, openid: openid, atype: 1});
    let isZan = false;
    if(reply){
      isZan = true;
    }
    let json = { articleid, atype: 1};
    let result = await service.normal.getinfos('Reply',json,page,size);
    let temp = result.result.result;
    ctx.body = { data:1,content: { isZan, data: temp }};
  }
  async getping() {
    const {ctx,service} = this;
    const {articleid,page,size} = ctx.request.body;
    let json = { articleid, atype: 2};
    let result = await service.normal.getinfos('Reply',json,page,size);
    ctx.body = result;
  }

  async getliuyan() {
    const {ctx,service} = this;
    const {page,size} = ctx.request.body;
    let result = await service.normal.getinfos('Reply',{},page,size);
    ctx.body = result;
  }

  async lynewnum() {
    const {ctx,service} = this;
    let sum = await ctx.model.Reply.count({isShow: false});
    ctx.body = {data: 1,sum: sum};
  }

  async isShowliuyan(){
    const {ctx,service} = this;
    try {
      const {ids,types} = ctx.request.body;
      if(types==1){
        await ctx.model.Reply.update({ _id: mongoose.mongo.ObjectId(ids) }, {$set:{isShow: true}});
      }else{
        await ctx.model.Reply.updateMany({atype: 2,shoptoplvid: ctx.session.admin._id},{$set:{isShow: true}},false,true);
      }
      ctx.body = {data:1,content:'信息修改成功'};
    } catch (error) {
        console.log(error)
        return {data:0,content:error};
    }
  }


  async getspicle(){
    const {ctx,service} = this;
    const {state,limit} = ctx.request.body;
    let result = await service.normal.getlimits('Article',state,limit);
    ctx.body = result;
  }
  async getone() {
    const {ctx,service} = this;
    const {id} = ctx.request.body;
    let result = await service.normal.getinfobyid('Article',id);
    if(result.data==1){
      if(!result.result.isMe){
        let openid = result.result.openid;
        let userInfo = await service.user.getUserByid(openid);
        let visit_count = result.result.visit_count;
        visit_count++
        await service.normal.statebyid('Article',id,{visit_count});
        if(userInfo.data==1){
          let {nickName,avatarUrl} = userInfo.result;
          result.author = {nickName,avatarUrl};
        }
      }
    }
    ctx.body = result;
  }
  async updateone(){
    const {ctx,service} = this;
    const {id,form} = ctx.request.body;
    let result = await service.normal.updateinfobyid('Article',id,form);
    ctx.body = result;
  }
  async statedone() {
    const {ctx,service} = this;
    const {id,states} = ctx.request.body;
    let result = await service.normal.statebyid('Article',id,states);
    ctx.body = result;
  }
}

module.exports = ReplyController;